package libs.yzx.zaza.acts2;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2016/10/12.
 */
public class CanvasPathActivity extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final MyView mv = new MyView(this);
        mv.setPadding(8,8,8,8);
        mv.setBackgroundColor(Color.BLACK);
        setContentView(mv);

        ValueAnimator va = ValueAnimator.ofInt(0, 100);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                int nowProgress = (int) animation.getAnimatedValue();
                mv.setProgress(nowProgress);
            }
        });
        va.setDuration(2000);
        va.start();

    }



    public static class MyView extends View{
        public MyView(Context context) {
            super(context);
        }
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        {
            paint.setDither(true);
            paint.setColor(Color.WHITE);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeCap(Paint.Cap.ROUND);//线条是圆角
        }

        private Path path = new Path();


        private final int MAX_PROGRESS = 100;
        private int progress = 0;
        private RectF rect;

        @Override
        protected void onDraw(Canvas canvas) {
            if(rect == null){
                float strokeWidth = paint.getStrokeWidth();
                int minEdgeLen = Math.min(getHeight(),getWidth());
                int rectLen = minEdgeLen-(int)strokeWidth;
                int left = getWidth()>minEdgeLen ? (getWidth()/2-rectLen/2) : (int)strokeWidth/2;
                int top = getHeight()>minEdgeLen ? (getHeight()/2-rectLen/2) : (int)strokeWidth/2;
                rect = new RectF(left,top,left+rectLen,top+rectLen);
            }
            path.reset();
            path.addArc(rect,-90,(float)progress/(float)MAX_PROGRESS*360);
            paint.setStrokeWidth(15);
            canvas.drawPath(path,paint);

            //Path.Direction.CW 表示顺时针
            // float数组表示长方形左上到右下的圆角radius
            path.reset();
            path.addRoundRect(new RectF(400,400,600,600),new float[]{
                    5,10,15,20,25,30,35,40
            }, Path.Direction.CW);
            paint.setStrokeWidth(5);
            canvas.drawPath(path,paint);

        }

        public void setProgress(int p){
            this.progress = p;
            invalidate();
        }

    }

}


